package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.CheckGroup;
import com.itheima.message.MessageConstant;
import com.itheima.result.PageResult;
import com.itheima.result.QueryPageBean;
import com.itheima.result.Result;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        }catch (Exception ex){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult=checkGroupService.findPage(currentPage,pageSize,queryString);
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        CheckGroup checkGroup=checkGroupService.findById(id);
        if (checkGroup!=null){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        try {
            List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            System.out.println(checkitemIds);
           return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            System.out.println(checkitemIds.length);
            checkGroupService.edit(checkGroup,checkitemIds);
        }catch (Exception ex){
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            boolean flag = checkGroupService.delete(id);
            if (flag){
                return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }else {
                return new Result(false,"数据被关联，无法删除");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
            List<CheckGroup> checkGroupList=checkGroupService.findAll();
            if (checkGroupList!=null&&checkGroupList.size()>0){
                return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkGroupList);
            }
            return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}
